import { QueryClient, useMutation } from "@tanstack/react-query";
import { useForm } from "react-hook-form";
import { AxiosError } from "axios";
import { ModelFileService } from "@/shared/api/api.model-file";


export function useUploadModelFile() {
    const { register, handleSubmit } = useForm<{
        file: File[]
    }>();
    const queryClient = new QueryClient

    const uploadModelFileMutation = useMutation(
        {
            mutationFn: ModelFileService.uploadFile,
            onError: (error: (AxiosError<{ message: string }>)) => error,
        }, queryClient
    );
    const isError = uploadModelFileMutation.isError;

    return {
        register,
        isError,
        response: uploadModelFileMutation.data,
        isPending: uploadModelFileMutation.isPending,
        isSuccess: uploadModelFileMutation.isSuccess,
        handleSubmit: handleSubmit((data) => uploadModelFileMutation.mutate({ ...data })),
    };
}